<template>
  <div class="app-container">
    <el-row :gutter="20">

      <!--用户数据-->
      <el-col :span="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="用户账号" prop="userName">
            <el-input v-model="queryParams.userName" placeholder="请输入用户账号" clearable size="small" style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号码" clearable size="small"
              style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="用户状态" clearable size="small" style="width: 240px">
              <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>
          <el-form-item label="注册时间">
            <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd"
              type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['system:user:add']">新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:user:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:user:remove']"
            >删除</el-button>
          </el-col> -->
          <!-- <el-col :span="1.5">
            <el-button
              type="info"
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              v-hasPermi="['system:user:import']"
            >导入</el-button>
          </el-col> -->
          <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              :loading="exportLoading"
              @click="handleExport"
              v-hasPermi="['system:user:export']"
            >导出</el-button>
          </el-col> -->
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="头像" align="center" key="avatar" prop="avatar">
            <template slot-scope="scope">
              <el-image v-if="scope.row.avatar" style="width: 40px; height: 40px;border-radius: 50%;"
                :src="scope.row.avatar">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="账号" align="center" key="userName" prop="userName" v-if="columns[1].visible"
            :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-button type="text" @click="handleShow(scope.row)">{{ scope.row.userName }}</el-button>
            </template>
          </el-table-column>
          <el-table-column label="名称" align="left" key="nickName" prop="nickName" v-if="columns[2].visible"
            :show-overflow-tooltip="true" />
          <!-- <el-table-column label="用户性别" align="center" prop="sex" :formatter="sexFormat" /> -->
          <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" v-if="columns[4].visible"
            width="120" />
          <!-- <el-table-column label="所属公司" align="center" prop="company.name" :show-overflow-tooltip="true" /> -->
          <el-table-column label="所属代理" align="center" key="proxy.name" prop="proxy.name"
            :show-overflow-tooltip="true" />

          <!-- <el-table-column label="个人技能" align="center" prop="tags" /> -->
          <el-table-column label="状态" align="center" key="status" v-if="columns[5].visible">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.status" active-value="0" inactive-value="1"
                @change="handleStatusChange(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="注册时间" align="center" prop="createTime" v-if="columns[6].visible" width="170">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="积分" align="center" prop="integral" />
          <el-table-column label="余额" align="center" prop="amount" />
          <el-table-column label="收入" align="center" prop="inFee" />
          <el-table-column label="支出" align="center" prop="outFee" />
          <el-table-column label="操作" align="center" width="260" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-document"
                @click="handleOrderList(scope.row)">订单信息</el-button>

              <el-button size="mini" type="text" icon="el-icon-search" @click="handleShow(scope.row)">查看</el-button>
              <el-button size="mini" type="text" icon="el-icon-map-location"
                @click="handleAddress(scope.row)">地址</el-button>
              <el-button size="mini" type="text" icon="el-icon-bank-card"
                @click="handleBankcard(scope.row)">银行卡</el-button>
              <el-button size="mini" type="text" icon="el-icon-bank-card"
                @click="HandleRole(scope.row)">设置经理</el-button>
              <!--
              <el-button
                v-if="scope.row.userId !== 1"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:user:remove']"
              >删除</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-key"
                @click="handleResetPwd(scope.row)"
                v-hasPermi="['system:user:resetPwd']"
              >重置</el-button> -->
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="disabled">
        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.userId == undefined" label="用户账号" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入用户账号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.userId == undefined" label="用户密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入用户密码" type="password" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入手机号码" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="用户性别">
              <el-select v-model="form.sex" placeholder="请选择">
                <el-option v-for="dict in sexOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel
                }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

          <el-col :span="12">
            <el-form-item label="角色">
              <el-select v-model="form.roleIds" multiple placeholder="请选择">
                <el-option v-for="item in roleOptions" :key="item.roleId" :label="item.roleName" :value="item.roleId"
                  :disabled="item.status == 1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!disabled">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="地址列表" :visible.sync="addressOpen" width="50%" append-to-body>
      <el-table :data="addressList">
        <el-table-column label="用户" align="center" prop="userName" />
        <el-table-column label="电话" align="center" prop="userTelephone" />
        <el-table-column label="区域" align="center" prop="area" />
        <el-table-column label="详细地址" align="center" prop="detail" width="300" />
        <!-- <el-table-column label="经度" align="center" prop="x" /> -->
        <!-- <el-table-column label="纬度" align="center" prop="y" /> -->
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="订单信息" :visible.sync="orderOpen" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="true">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名称" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入用户名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入手机号码" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色">
              <el-select v-model="form.roleIds" multiple placeholder="请选择">
                <el-option v-for="item in roleOptions" :key="item.roleId" :label="item.roleName" :value="item.roleId"
                  :disabled="item.status == 1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-tabs>
              <el-tab-pane label="下单记录">
                <el-table :data="orderList">
                  <el-table-column label="订单编号" align="center" prop="num" width="170">
                  </el-table-column>
                  <el-table-column label="订单信息" align="center" prop="attribute" width="170" />
                  <el-table-column label="预约时间" align="center" prop="orderTime" width="180">
                    <template slot-scope="scope">
                      <span>{{ parseTime(scope.row.orderTime) }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="接单人" align="center" prop="receiveName" />
                  <el-table-column label="联系电话" align="center" prop="telephone" />
                  <el-table-column label="实付" align="center" prop="pay" />
                </el-table>
                <pagination v-show="orderTotal > 0" :total="orderTotal" :page.sync="queryOrderParams.pageNum"
                  :limit.sync="queryOrderParams.pageSize" @pagination="getOrderList" />
              </el-tab-pane>
              <el-tab-pane label="接单记录">
                <el-table :data="receiveList">
                  <el-table-column label="订单编号" align="center" prop="num" width="170">
                  </el-table-column>
                  <el-table-column label="订单信息" align="center" prop="attribute" width="170" />
                  <el-table-column label="预约时间" align="center" prop="orderTime" width="180">
                    <template slot-scope="scope">
                      <span>{{ parseTime(scope.row.orderTime) }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="联系人" align="center" prop="username" />
                  <el-table-column label="联系电话" align="center" prop="telephone" />
                  <el-table-column label="实付" align="center" prop="pay" />
                </el-table>
                <pagination v-show="receiveTotal > 0" :total="receiveTotal" :page.sync="queryReceiveParams.pageNum"
                  :limit.sync="queryReceiveParams.pageSize" @pagination="getReceiveList" />
              </el-tab-pane>
            </el-tabs>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="银行卡列表" :visible.sync="bankcardOpen" width="50%" append-to-body>
      <el-table :data="bankcardList">
        <el-table-column label="银行名称" align="center" prop="bankName" />
        <el-table-column label="账户名" align="center" prop="account" />
        <el-table-column label="卡号" align="center" prop="cardNo" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="设置经理" :visible.sync="setRoleOpen" width="50%" append-to-body>
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="140px" :disabled="disabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设置经理" prop="inviteRole">
              <el-radio v-model.number="roleForm.inviteRole" :label="1">经理</el-radio>
              <el-radio v-model.number="roleForm.inviteRole" :label="10">非经理</el-radio>
            </el-form-item>
            <el-form-item label="最大分成比例" prop="inviteMaxPercent">
              <el-input  v-model.number="roleForm.inviteMaxPercent"  placeholder="请输入数字" size="small" style="width: 240px">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSetinIteRole" v-if="!disabled">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser, exportUser, changeUserStatus, importTemplate, updateInviteRole } from "@/api/system/user";
import { getToken } from "@/utils/auth";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listUserAddress } from "@/api/order/address";
import { listUserOrder, listUserReceive } from "@/api/business/order";
import { listUserBankcard } from "@/api/user/bankcard";
export default {
  name: "User",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      disabled: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      orderTotal: 0,
      receiveTotal: 0,
      // 用户表格数据
      userList: null,
      addressList: null,
      orderList: null,
      receiveList: null,
      bankcardList: null,
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      addressOpen: false,
      orderOpen: false,
      bankcardOpen: false,
      setRoleOpen: false,
      // 部门名称
      deptName: undefined,
      // 默认密码
      initPassword: undefined,
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [],
      // 性别状态字典
      sexOptions: [],
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/user/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined
      },
      queryOrderParams: {
        pageNum: 1,
        pageSize: 5,
      },
      queryReceiveParams: {
        pageNum: 1,
        pageSize: 5,
      },
      // 列信息
      columns: [
        { key: 0, label: `用户编号`, visible: true },
        { key: 1, label: `用户名称`, visible: true },
        { key: 2, label: `用户昵称`, visible: true },
        { key: 3, label: `部门`, visible: true },
        { key: 4, label: `手机号码`, visible: true },
        { key: 5, label: `状态`, visible: true },
        { key: 6, label: `创建时间`, visible: true }
      ],
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "用户密码不能为空", trigger: "blur" }
        ],
        phonenumber: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        inviteMaxPercent: [
          {
            validator: (rule, value, callback) => {
              // 校验必填
              if (value === '' || value == null) {
                return callback(new Error('请输入数字'));  // 必填校验
              }

              // 校验是否是数字
              if (isNaN(value)) {
                return callback(new Error('必须为数字'));
              }

              // 校验最大值
              if (value > 100) {
                return callback(new Error('最大值不能超过 100'));
              }

              // 校验最小值
              if (value < 0) {
                return callback(new Error('最小值不能小于 0'));
              }

              // 校验通过
              callback();
            },
            trigger: 'blur'
          }
        ]
      },
      roleForm: {
        inviteRole: 10,
        inviteMaxPercent: 6,
        userId: ''
      }
    };
  },

  created() {
    this.getList();
    // this.getTreeselect();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
    this.getConfigKey("sys.user.initPassword").then(response => {
      // this.initPassword = response.msg;
    });
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      }
      );
    },
    // 用户性别字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },

    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.userName + '"用户吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return changeUserStatus(row.userId, row.status);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.addressOpen = false;
      this.orderOpen = false;
      this.bankcardOpen = false;
      this.setRoleOpen = false;
      this.roleForm = {
        inviteRole: 10,
        inviteMaxPercent: 6,
        userId: ''
      }
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: []
      };
      this.resetForm("form");
      this.disabled = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams)
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getUser().then(response => {
        this.postOptions = response.posts;
        this.roleOptions = response.roles;
        this.open = true;
        this.title = "添加用户";
        this.form.password = this.initPassword;
      });
    },
    /** 修改按钮操作 */
    handleShow(row) {
      this.reset();
      this.disabled = true;
      const userId = row.userId || this.ids;
      getUser(userId).then(response => {
        this.form = response.data;
        this.postOptions = response.posts;
        this.roleOptions = response.roles;
        this.form.postIds = response.postIds;
        this.form.roleIds = response.roleIds;
        this.open = true;
        this.title = "查看用户";
        this.form.password = "";
      });
    },
    /** 查看地址操作 */
    handleAddress(row) {
      const userId = row.userId || this.ids;
      listUserAddress({ userId: userId }).then(response => {
        this.addressList = response.data;
        this.addressOpen = true;
      });
    },
    /** 查看银行卡操作 */
    handleBankcard(row) {
      const userId = row.userId || this.ids;
      listUserBankcard({ userId: userId }).then(response => {
        this.bankcardList = response.data;
        this.bankcardOpen = true;
      });
    },
    /**
     * 
     * @param row 打开设置弹窗
     */
    HandleRole(row) {
      const userId = row.userId || this.ids;
      this.setRoleOpen =true;
      this.$nextTick(() => {
        getUser(userId).then(response => {
          this.roleForm.inviteRole = response.data.inviteRole != null ? response.data.inviteRole : 10
          this.roleForm.inviteMaxPercent = response.data.inviteMaxPercent != null ? response.data.inviteMaxPercent : 6
          this.roleForm.userId = userId
        });
      });
    },

    /**
     * 提交设置表单
     */
    submitSetinIteRole() {
      updateInviteRole(this.roleForm).then(
        response => {
          if (response.code === 200) {
            this.setRoleOpen =false;
            this.$message({
              showClose: true,
              message: response.msg,
              type: 'success'
            });
          }
        }
      )
    },



    /** 查看地址操作 */
    handleOrderList(row) {
      const userId = row.userId || this.ids;
      this.orderOpen = true;
      this.orderList = null;
      this.receiveList = null;
      this.$nextTick(() => {
        getUser(userId).then(response => {
          this.form = response.data;
          this.postOptions = response.posts;
          this.roleOptions = response.roles;
          this.form.postIds = response.postIds;
          this.form.roleIds = response.roleIds;
          this.getOrderList();
          this.getReceiveList();
        });
      });

    },
    /** 查询订单管理列表 */
    getOrderList() {
      this.queryOrderParams.userId = this.form.userId;
      listUserOrder(this.queryOrderParams).then(response => {
        this.orderList = response.rows;
        this.orderTotal = response.total;
      });
    },
    /** 查询订单管理列表 */
    getReceiveList(id) {
      this.queryReceiveParams.receiveId = this.form.userId;
      listUserReceive(this.queryReceiveParams).then(response => {
        this.receiveList = response.rows;
        this.receiveTotal = response.total;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != undefined) {
            updateUser(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$confirm('是否确认删除用户编号为"' + userIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delUser(userIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportUser(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then(response => {
        this.download(response.msg);
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
